package com.gulimall.product.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gulimall.common.enums.AttrEnum;
import com.gulimall.product.entity.*;
import com.gulimall.product.service.*;
import com.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.gulimall.common.utils.R;


/**
 * 属性分组
 *
 * @author ht
 * @email ht035200@163.com
 * @date 2020-04-08 11:13:29
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(AttrGroupEntity attrGroup, @RequestParam(name = "page", defaultValue = "1") Integer pageNo,
                  @RequestParam(name = "limit", defaultValue = "10") Integer pageSize, @RequestParam(name = "key") String key) {
        IPage<AttrGroupEntity> queryPage = attrGroupService.queryPage(new Page<>(pageNo, pageSize), attrGroup, key);
        List<AttrGroupEntity> attrGroupEntities = queryPage.getRecords();
        for (AttrGroupEntity attrGroupEntity : attrGroupEntities) {
            attrGroupEntity.setCatelogIdsAndNames(categoryService.listWithTreeByCatId(attrGroupEntity.getCatelogId()));
        }
        return R.ok().put("page", queryPage);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    public R listByCatId(@PathVariable(value = "catId", required = false) Long catId, @RequestParam(name = "page", defaultValue = "1") Integer pageNo,
                         @RequestParam(name = "limit", defaultValue = "10") Integer pageSize, @RequestParam(name = "key", required = false) String key) {
        AttrGroupEntity attrGroup = new AttrGroupEntity();
        if (catId != 0) {
            attrGroup.setCatelogId(catId);
        }
        IPage<AttrGroupEntity> queryPage = attrGroupService.queryPage(new Page<>(pageNo, pageSize), attrGroup, key);
        List<AttrGroupEntity> attrGroupEntities = queryPage.getRecords();
        for (AttrGroupEntity attrGroupEntity : attrGroupEntities) {
            attrGroupEntity.setCatelogIdsAndNames(categoryService.listWithTreeByCatId(attrGroupEntity.getCatelogId()));
        }
        return R.ok().put("page", queryPage);
    }

    /**
     * 列表
     */
    @RequestMapping("/getAllDataList")
    public R getAllDataList(AttrGroupEntity attrGroup, @RequestParam(name = "page", defaultValue = "1") Integer pageNo, @RequestParam(name = "limit", defaultValue = "10") Integer pageSize, @RequestParam(name = "key") String key) {
        IPage<AttrGroupEntity> allDataPage = attrGroupService.queryAllDataPage(new Page<>(pageNo, pageSize), attrGroup, key);
        List<AttrGroupEntity> attrGroupEntities = allDataPage.getRecords();
        for (AttrGroupEntity attrGroupEntity : attrGroupEntities) {
            attrGroupEntity.setCatelogIdsAndNames(categoryService.listWithTreeByCatId(attrGroupEntity.getCatelogId()));
        }
        return R.ok().put("page", allDataPage);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        if (attrGroup != null) {
            CategoryEntity category = categoryService.getById(attrGroup.getCatelogId());
            attrGroup.setCatelogName(category.getName());
            attrGroup.setCatelogIdsAndNames(categoryService.listWithTreeByCatId(attrGroup.getCatelogId()));
        }
        return R.ok().put("attrGroup", attrGroup);
    }

    @GetMapping("{attrGroupId}/attr/relation")
    public R getRelationByAttrGroupId(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        List<AttrEntity> attrEntityList = new ArrayList<>();
        if (attrGroup != null) {
            List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationService.list(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupId));
            for (AttrAttrgroupRelationEntity relationEntity : relationEntities) {
                Long attrId = relationEntity.getAttrId();
                LambdaQueryWrapper<AttrEntity> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.select(AttrEntity::getAttrId, AttrEntity::getAttrName, AttrEntity::getValueSelect);
                List<AttrEntity> list = attrService.list(queryWrapper.eq(AttrEntity::getAttrId, attrId));
                attrEntityList.addAll(list);
            }
        }
        return R.ok().put("data", attrEntityList);
    }

    @GetMapping("{attrGroupId}/noattr/relation")
    public R getNoAttrRelationByAttrGroupId(@PathVariable("attrGroupId") Long attrGroupId, @RequestParam(name = "page", defaultValue = "1") Integer pageNo,
                                            @RequestParam(name = "limit", defaultValue = "10") Integer pageSize, @RequestParam(name = "key", required = false) String key) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        IPage<AttrEntity> queryPage = attrService.queryPageByCateId(new Page<>(pageNo, pageSize), null, attrGroup.getCatelogId(), key, AttrEnum.ATTR_TYPE_BASE.getType());
        return R.ok().put("page", queryPage);
    }

    @PostMapping("/attr/relation")
    @Transactional(rollbackFor = Exception.class)
    public R addRelationByAttrGroupId(@RequestBody List<AttrAttrgroupRelationEntity> relationEntities) {
        attrAttrgroupRelationService.saveBatch(relationEntities);
        return R.ok();
    }

    @PostMapping("/attr/relation/delete")
    @Transactional(rollbackFor = Exception.class)
    public R removeRelationByAttrGroupId(@RequestBody List<AttrAttrgroupRelationEntity> relationEntities) {
        for (AttrAttrgroupRelationEntity relationEntity : relationEntities) {
            attrAttrgroupRelationService.remove(new LambdaUpdateWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrGroupId, relationEntity.getAttrGroupId()).eq(AttrAttrgroupRelationEntity::getAttrId, relationEntity.getAttrId()));
        }
        return R.ok();
    }

    @GetMapping("/{catId}/withattr")
    public R withAttrByAttrGroupId(@PathVariable("catId") Long catId) {
        List<AttrGroupWithAttrVo> attrGroupWithAttrs = attrGroupService.getAttrGroupWithAttrs(catId);
        return R.ok().put("data",attrGroupWithAttrs);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        List<Long> ids = Arrays.asList(attrGroupIds);
        attrGroupService.removeByIds(ids);
        for (Long id : ids) {
            attrAttrgroupRelationService.remove(new LambdaUpdateWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrGroupId, id));
        }
        return R.ok();
    }

}
